#include "vsocket.h"
#include <string.h>
vsock_t::vsock_t(socket_t sock)
    : __sock(sock)
{

}

void vsock_t::send(const void *data, const size_t length) const
{
    int total = 0;
    int ws = 0;

    while(total < length)
    {
        ws = ::send(__sock, data + total, length - total, 0);
        if (-1 == ws)
        {
            //ADD_STD_ERR;
            break;
        }
        total += ws;
    }
}

void vsock_t::recv(void *data, const size_t length) const
{
    int total = 0;
    int wr = 0;

    while (total < length)
    {
        wr = ::recv(__sock, data + total, length - total, 0);

        if (-1 == wr)
        {
            //ADD_STD_ERR;
            break;
        }

        total += wr;
    }
}

inline void vsock_t::send_char(const char *data) const
{
    size_t len = strlen(data);
    if (len == 0)
    {
        //ADD_ERR("0 bytes");
        return;
    }

    send_numeric<size_t>(len);
    send(data, strlen(data));
}

inline char* vsock_t::recv_char() const
{
    size_t len = recv_numeric<size_t>();
    if (len == 0)
    {
        //ADD_ERR("reciving 0 bytes");
        return 0;
    }

    char *buffer = new char[len];

    recv(buffer, len);
    return buffer;
}

int vsock_t::operator=(const int num)
{
    return (__sock = num);
}
